package com.lancea12.buster.android;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class LoginActivity extends Activity {
    /** Called when the activity is first created. */
    
    private Button loginButton;
    private Button finishButton;
    private TextView user;
    private TextView password;
    private Resources res;
    private TextView results;
    private Handler mHandler;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        res = getResources();
        user = (TextView)findViewById(R.id.userName);
        password = (TextView)findViewById(R.id.password);
    	results = (TextView)findViewById(R.id.results);
    	results.setText("<results will go here>");
    	mHandler = new Handler(){
    		public void handleMessage(Message msg) {
				results.setText(msg.getData().getString("response"));
    			//LoginActivity.this.finish();
    		};
    	};
        
        loginButton = (Button)findViewById(R.id.loginButton);
        loginButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				results.setText("waiting");
				LoginNetwork net = new LoginNetwork(
						user.getText().toString(), 
						password.getText().toString(), 
						mHandler,
						LoginActivity.this);
		    	Thread login = new Thread(net);
		    	login.start();
		    	
		    	while(login.isAlive()){
		    		try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
		    	}
			}

		});
        finishButton = (Button)findViewById(R.id.finishButton);
        finishButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}

		});
        
    }
    

    
}